// pages/videocard/videocard.js
const app = getApp();
const db = wx.cloud.database();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    correspondingInfo: "",
    catalog: [],
    isCollect: false,
    canTapCollect: true,
  },
  getBackclass() {
    wx.navigateBack({
      delta: 1,
      fail: res => {
        wx.switchTab({
          url: '/pages/home/home',
        })
      }
    })
  },

  showfirstvideo(e) {
    console.log(e);
    let obj = {
      correspondinginfomation: this.data.correspondingInfo,
      index: 0
    };
    app.navTmpInfo = obj;
    wx.navigateTo({
      url: '/pages/showcoursevideo/showcoursevideo',
    })

  },
  transforshowcoursevideo(e) {
    let obj = {
      correspondinginfomation: e.currentTarget.dataset.correspondinginfomation,
      index: e.currentTarget.dataset.index
    }
    getApp().navTmpInfo = obj;
    wx.navigateTo({
      url: "/pages/showcoursevideo/showcoursevideo"
    })
  },
  collectionCourse() {
    let that = this;
    // 先判断是否允许点击收藏
    if (that.data.canTapCollect == false) {
      wx.showToast({
        icon: "none",
        title: '操作频繁',
      })
      return;
    }

    // 没收藏，现在收藏
    if (that.data.isCollect == false) {
      that.setData({
        isCollect: true
      });
      db.collection('userCollect').add({
        data: {
          type: '课程',
          uuid: that.data.correspondingInfo._id
        },
        success: res => {
          console.log(res);
        },
        fail: res => {
          console.log(res);
        }
      })
    } else {
      // 收藏了，现在取消收藏
      that.setData({
        isCollect: false
      });
      db.collection('userCollect').where({
        _openid: app.personInfo._openid,
        type: '课程',
        uuid: that.data.correspondingInfo._id
      }).remove();
    }

    // 不允许再点击
    this.setData({
      canTapCollect: false
    })
    // 防止频繁操作的定时器
    // N秒后再允许点击
    setTimeout(() => {
      that.setData({
        canTapCollect: true
      });
    }, 2000);

  },


  // ---------------------------------------------------
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this;
    console.log(options);
    // 通过别人分享，点击进来的
    if (options._id) {
      let id = options._id;
      console.log(id);
      db.collection('course').doc(id).get({
        success: res => {
          if (res.data.length != 0) {
            console.log(res.data);
            that.setData({
              correspondingInfo: res.data
            })
            //查询用户是否有收藏这个课程
            db.collection('userCollect').where({
              _openid: app.personInfo._openid,
              uuid: that.data.correspondingInfo._id
            }).get({
              success: res => {
                if (res.data.length != 0) {
                  that.setData({
                    isCollect: true
                  })
                }
              },
              fail: res => {
                console.log(res);
              }
            })
          }

        },
        fail: res => {
          console.log(res);
        }
      })

    } else {
      // 用户自主进来的
      if (!Object.keys(app.navTmpInfo).length) {
        wx.showToast({
          icon: 'none',
          title: '异常',
        });
        return;
      }

      this.setData({
        correspondingInfo: app.navTmpInfo
      })
      console.log(this.data.correspondingInfo);

      //查询用户是否有收藏这个课程
      db.collection('userCollect').where({
        _openid: app.personInfo._openid,
        uuid: this.data.correspondingInfo._id
      }).get({
        success: res => {
          if (res.data.length != 0) {
            that.setData({
              isCollect: true
            })
          }
        },
        fail: res => {
          console.log(res);
        }
      })
    }



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var correspondingInfo = this.data.correspondingInfo;
    var _id = correspondingInfo._id;
    var path = "pages/videocard/videocard?_id=" + _id;
    console.log(path);
    return {
      title: "农学僧 - 视频卡片 - " + correspondingInfo.coursetitle,
      path: path,
      imageUrl: correspondingInfo.classimage,
    }
  }
})